package SGEngine

import (
	//"fmt"
	"github.com/gl"
	"reflect"
	"strconv"
)

const (
	BUFFER_Quad = 1
	BUFFER_Cube = 2
)

var (
	ACBufferMgr BufferMgr
)

func init() {
	ACBufferMgr.VertexBufferCache = make(map[uint32]gl.Buffer)
	ACBufferMgr.TexBufferCache = make(map[uint32]gl.Buffer)

}

type BufferMgr struct {
	VertexBufferCache map[uint32]gl.Buffer
	TexBufferCache    map[uint32]gl.Buffer
}

func (b *BufferMgr) GetVertexBuffer(buffertype uint32) gl.Buffer {
	v, k := b.VertexBufferCache[buffertype]
	if k {
		return v
	}
	rbf := reflect.ValueOf(b).MethodByName("GenBuffer_Vertex_" + strconv.Itoa(int(buffertype))).Call([]reflect.Value{})[0]
	return gl.Buffer(rbf.Uint())

}

//BUFFER_Quad = 1
func (b *BufferMgr) GenBuffer_Vertex_1() gl.Buffer {
	BValue := gl.GenBuffer()
	BValue.Bind(gl.ARRAY_BUFFER)
	array := []float32{
		-0.5, -0.5, 0.0, //Bottom Left
		0.5, -0.5, 0.0, //Bottom Right
		-0.5, 0.5, 0.0, //Top Left
		0.5, 0.5, 0.0, //Top Right
	}
	gl.BufferData(gl.ARRAY_BUFFER, 12*4, array, gl.STATIC_DRAW)
	b.VertexBufferCache[BUFFER_Quad] = BValue
	return b.VertexBufferCache[BUFFER_Quad]
}

//BUFFER_Cube = 1
func (b *BufferMgr) GenBuffer_Vertex_2() gl.Buffer {
	BValue := gl.GenBuffer()
	BValue.Bind(gl.ARRAY_BUFFER)
	array := []float32{
		-0.5, -0.5, 0.5,
		0.5, -0.5, 0.5,
		-0.5, -0.5, -0.5,
		0.5, -0.5, 0.5,
		0.5, -0.5, -0.5,
		-0.5, -0.5, -0.5, //底部

		-0.5, 0.5, 0.5,
		0.5, 0.5, 0.5,
		-0.5, 0.5, -0.5,
		0.5, 0.5, 0.5,
		0.5, 0.5, -0.5,
		-0.5, 0.5, -0.5, //顶部

		-0.5, -0.5, 0.5,
		0.5, -0.5, 0.5,
		-0.5, 0.5, 0.5,
		0.5, -0.5, 0.5,
		0.5, 0.5, 0.5,
		-0.5, 0.5, 0.5, //前面

		-0.5, -0.5, -0.5,
		0.5, -0.5, -0.5,
		-0.5, 0.5, -0.5,
		0.5, -0.5, -0.5,
		0.5, 0.5, -0.5,
		-0.5, 0.5, -0.5, //后面

		-0.5, -0.5, -0.5,
		-0.5, -0.5, 0.5,
		-0.5, 0.5, -0.5,
		-0.5, -0.5, 0.5,
		-0.5, 0.5, 0.5,
		-0.5, 0.5, -0.5, //左面

		0.5, -0.5, -0.5,
		0.5, -0.5, 0.5,
		0.5, 0.5, -0.5,
		0.5, -0.5, 0.5,
		0.5, 0.5, 0.5,
		0.5, 0.5, -0.5, //右面
	}
	gl.BufferData(gl.ARRAY_BUFFER, 108*4, array, gl.STATIC_DRAW)
	b.VertexBufferCache[BUFFER_Cube] = BValue
	return BValue
}

func (b *BufferMgr) GetTexBuffer(buffertype uint32) gl.Buffer {
	v, ok := b.TexBufferCache[buffertype]
	if ok {
		return v
	}
	rbf := reflect.ValueOf(b).MethodByName("GenBuffer_Tex_" + strconv.Itoa(int(buffertype))).Call([]reflect.Value{})[0]
	return gl.Buffer(rbf.Uint())
}

//BUFFER_Quad = 1
func (b *BufferMgr) GenBuffer_Tex_1() gl.Buffer {
	BValue := gl.GenBuffer()
	BValue.Bind(gl.ARRAY_BUFFER)
	array := []float32{
		0, 0,
		1, 0,
		0, 1,
		1, 1,
	}
	gl.BufferData(gl.ARRAY_BUFFER, 8*4, array, gl.STATIC_DRAW)
	b.TexBufferCache[BUFFER_Quad] = BValue
	return b.TexBufferCache[BUFFER_Quad]
}

//BUFFER_Cube = 1
func (b *BufferMgr) GenBuffer_Tex_2() gl.Buffer {
	BValue := gl.GenBuffer()
	BValue.Bind(gl.ARRAY_BUFFER)
	array := []float32{
		0, 0,
		1, 0,
		0, 1,
		1, 0,
		1, 1,
		0, 1,

		0, 0,
		1, 0,
		0, 1,
		1, 0,
		1, 1,
		0, 1,

		0, 0,
		1, 0,
		0, 1,
		1, 0,
		1, 1,
		0, 1,

		0, 0,
		1, 0,
		0, 1,
		1, 0,
		1, 1,
		0, 1,

		0, 0,
		1, 0,
		0, 1,
		1, 0,
		1, 1,
		0, 1,

		0, 0,
		1, 0,
		0, 1,
		1, 0,
		1, 1,
		0, 1,
	}
	gl.BufferData(gl.ARRAY_BUFFER, 72*4, array, gl.STATIC_DRAW)
	b.TexBufferCache[BUFFER_Cube] = BValue
	return b.TexBufferCache[BUFFER_Cube]
}
